﻿using System;
using System.Globalization;
using System.Web.UI;
using System.Web.UI.WebControls;
using AB.ServiceDirectory.BO;
using AB.ServiceDirectory.Commons;
using AB.ServiceDirectory.Commons.DataFilters;

namespace AB.ServiceDirectory.Design.Org
{
    public partial class AOrganisationList : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
               // LoadAllOrganisation();
                var filter = new AOrganisationFilter();
                ViewState["filter"] = filter;
             
            }
            
        }
        //private   void LoadAllOrganisation()
        //{
        //    GridViewOrg.DataSource = AOrganisationBO.FindAllOrgarisation();
        //    GridViewOrg.DataBind();
        //}

        protected void FilterByLetters_Command(object sender, CommandEventArgs e)
        {
            var filter = ViewState["filter"] as AOrganisationFilter;
            if (filter != null)
            {
                var beginningLetters = StringTools.GetLetterGroup(e.CommandName);
                filter.SetBeginningLetters(beginningLetters);
                GridViewOrg.PageIndex = 0;
                GridViewOrg.DataBind();
            }
        }

        protected void CheckBoxInactive_CheckedChanged(object sender, EventArgs e)
        {
            var filter = ViewState["filter"] as AOrganisationFilter;
            if(filter!=null)
            {
                filter.IsActive = CheckBoxInactive.Checked;
                GridViewOrg.DataBind();
            }
        }

        protected void OrganizationObjectDataSource_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)
        {
            var filter = ViewState["filter"] as AOrganisationFilter;
            e.InputParameters["filter"] = filter;
        }

        protected void GridViewOrg_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowIndex < 0)
            {
                return;
            }
            //hiden ID 
           // e.Row.Cells[0].Visible = false;

            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                  //string id = GridViewOrg.DataKeys[e.Row.RowIndex].Value.ToString();
              //  LinkButton linkButton = (LinkButton) GridViewOrg.FindControl("LinkButtonOrganisationName");
                //e.Row.Attributes["onClientClick"] = ClientScript.GetPostBackClientHyperlink(GridViewOrg,
                //                                                                     "Select$" + e.Row.RowIndex);
                e.Row.Attributes["onmouseover"] = "this.style.cursor='pointer';this.style.textDecoration='underline';";
                e.Row.Attributes["onmouseout"] = "this.style.textDecoration='none';";
                //e.Row.Attributes["onclick"] = "return confirm_Edit();";
                                                 
            }
        }

        protected void GridViewOrg_SelectedIndexChanged(object sender, EventArgs e)
        {
         
           
            //LinkButton09.Text = GridViewOrg.SelectedValue.ToString();
             
            
        }

        protected void LinkButtonOrganisationName_Command(object sender, CommandEventArgs e)
        {
            int organisationId = Convert.ToInt32(e.CommandArgument, CultureInfo.CurrentCulture);
            var organisation = AOrganisationBo.GetListOrganisationId(organisationId);

            if (organisation.IsActive)
            {
                Response.Redirect(string.Format("~/Organisation/EditAOrganisation.aspx?id={0}", organisationId));
            }
            else
            {
               // GridViewOrg.Attributes.Add("Onclick", "return confirm_Edit();");
                LinkButton linkButton = ((LinkButton)sender);
                Page.ClientScript.GetPostBackEventReference(linkButton, "onclick");
                linkButton.OnClientClick = "return confirm('Do you want to make this Supporting Materials active?')";
                //linkButton.Attributes.Add("onclick", "return Confirm_Edit() ");
            }
        }
    }
}